// pages/commentdetail/commentdetail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    commentId:null,//帖子id
    commentDetatil:null,// 帖子详情

    replayDialog:false, // 回复输入框显示
    replayInput:"",// 回复内容
    replayPid:null,// 回复的pid
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      commentId : options.id
    })

    this.getForumDetailById(options.id)

    // 通过帖子id获取帖子评论
    this.getForumPostCommentById(options.id)
     
  },

  


  // 点击行 回复
  tapItem(e){
   
    this.setData({
      replayDialog:true,
      replayPid:e.detail.id
    })
    
  },

  /**
   * 回复内容
   */
  replayInput(e){
    
    this.setData({
      replayInput:e.detail.value
    })
  },

  // 评论
  toComment(){
    this.setData({
      replayPid:0,
      replayDialog:true
    })
  },

  // 点击回复文字
  toReplay(e){
    this.setData({
      replayPid:e.currentTarget.dataset.id,
      replayDialog:true
    })
  },

  /**
   * 发送
   */
  doSend(){

    let param = {};
    param.pid = this.data.replayPid;
    param.content = this.data.replayInput;
    param.userId = app.globalData.userInfo.id;
    param.forumId = this.data.commentDetatil.id


    let that = this;
   
    app.util.request({
      url: "forum/commentForum",
      method: "post",
      data:param,
      showLoading:true,
      success: function (res) {

        if(res.data.code == 200){
          wx.showToast({
            title: '评论成功',
          })
          that.setData({
            replayDialog:false,
            replayInput:""
          })
          that.getForumPostCommentById(param.forumId)
        }
        
        
      },
      fail:function(){
        wx.showToast({
          title: '网路异常',
        })
      }
    })
  },

  /**
   * 通过帖子id获取帖子评论
   */
  getForumPostCommentById(postId){
    let that = this;
   
    app.util.request({
      url: "forum/getForumPostCommentById/"+postId,
      method: "get",
      showLoading:true,
      success: function (res) {

        if(res.data.code == 200){
          that.setData({
            commentList:res.data.data
          })
        }
        
        
      },
      fail:function(){
        wx.showToast({
          title: '网路异常',
        })
      }
    })
  },

  /**
   * 根据帖子id获取帖子详情
   *  forum/getForumDetailById
   */
  getForumDetailById(commentId){

    let that = this;
    let queryParam = "?forumId="+commentId;
    queryParam +="&userId="+app.globalData.userInfo.id;
    app.util.request({
      url: "forum/getForumDetailById"+queryParam,
      method: "post",
      showLoading:true,
      success: function (res) {

        if(res.data.code == 200){
          that.setData({
            commentDetatil:res.data.data
          })
        }
        
        
      },
      fail:function(){
        wx.showToast({
          title: '网路异常',
        })
      }
    })
  },


  /**
   * 收藏按钮
   */
  toCollect(){
    let detail = this.data.commentDetatil;
    if(detail.isCollect){
      // 取消收藏
      this.cancelCollect()
    }else{
      // 收藏
      this.doCollect()
    }

    detail.isCollect = detail.isCollect?false:true
    this.setData({
      commentDetatil:detail
    })
  },
 
  /**
   * 收藏
   */
  doCollect(){
    
    let param = {};
    param.forumId = this.data.commentId;
    param.userId = app.globalData.userInfo.id
    app.util.request({
      url: "forum/addForumCollect",
      method: "post",
      data:param,
      showLoading:true,
      success: function (res) {

        if(res.data.code == 200){
          wx.showToast({
            title: '收藏成功',
          })
        }
        
        
      },
      fail:function(){
        wx.showToast({
          title: '网路异常',
        })
      }
    })
  },
   /**
    * 取消收藏
    */
   cancelCollect(){ 
    let param = {};
    param.forumId = this.data.commentId;
    param.userId = app.globalData.userInfo.id
    app.util.request({
      url: "forum/cancelForumCollect",
      method: "post",
      data:param,
      showLoading:true,
      success: function (res) {

        if(res.data.code == 200){
          wx.showToast({
            title: '取消成功',
          })
        }
        
        
      },
      fail:function(){
        wx.showToast({
          title: '网路异常',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})